package org.dht.com.conf;

import java.util.HashMap;
import java.util.Map.Entry;

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * The <code>XmlAdapterHashMap</code> class implement mapped from
 * <code>HashMap<String, String></code> to <code>Params</code> and vice versa
 * 
 */
public final class XmlAdapterHashMap extends
		XmlAdapter<Params, HashMap<String, String>> {

	/**
	 * (non-Javadoc)
	 * 
	 * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
	 */
	public HashMap<String, String> unmarshal(Params params) throws Exception {

		HashMap<String, String> hashMap = new HashMap<String, String>();

		for (Param param : params.getParam()) {
			hashMap.put(param.getName(), param.getValue());
		}

		return hashMap;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
	 */
	public Params marshal(HashMap<String, String> hashMap) throws Exception {
		Params params = new Params();

		for (Entry<String, String> entry : hashMap.entrySet()) {
			Param param = new Param();
			param.setName((String) entry.getKey());
			param.setValue((String) entry.getValue());

			params.getParam().add(param);
		}

		return params;
	}

}
